package com.turing.common.util.uncompress;

import com.turing.common.util.uncompress.callback.ExtractCallback;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RarUncompress extends AbstractUncompress {

    @Override
    public void uncompress(String rarDir, String outDir) {

        RandomAccessFile randomAccessFile = null;
        IInArchive inArchive = null;
        try{
            // 第一个参数是需要解压的压缩包路径，第二个参数参考JdkAPI文档的RandomAccessFile
            randomAccessFile = new RandomAccessFile(rarDir, "r");
            inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));

            int[] in = new int[inArchive.getNumberOfItems()];
            for (int i = 0; i < in.length; i++) {
                in[i] = i;
            }
            inArchive.extract(in, false, new ExtractCallback(inArchive, outDir));
            inArchive.close();
            randomAccessFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /*public static void main(String[] args) {

        try {
            long l = System.currentTimeMillis();
            uncompress("D:\\安信资管安享未来号股票型FOF单一资产管理计划资产管理合同.rar","D:\\图灵\\");
            long l2 = System.currentTimeMillis();
            System.out.println(l2-l);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }*/
}
